﻿Public Partial Class Stock_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            ViewState("Color") = CreateDatatable()
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
            Dim ds As DataTable = clsGet.Get_ProductGroupMaster("", "L")
            If (ds.Rows.Count > 0) Then
                With ddlGroup
                    .ClearSelection()
                    .DataTextField = "Group_name"
                    .DataValueField = "Group_no"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
            ds = clsGet.Get_Color("")
            If (ds.Rows.Count > 0) Then
                With ddlColor
                    .ClearSelection()
                    .DataTextField = "Color_Name"
                    .DataValueField = "Color_Code"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
            If (Session("Stock") & "" <> "") Then
                ViewState("Stock") = Session("Stock") & ""
                ds = clsGet.Get_StockMaster("", "", Session("Stock") & "", "")
                If (ds.Rows.Count > 0) Then
                    txtGrade.Text = ds.Rows(0).Item("Grade")
                    txtBrand.Text = ds.Rows(0).Item("Brand")
                    txtDescription.Text = ds.Rows(0).Item("Description")
                    If Not (radioType.Items.FindByValue(ds.Rows(0).Item("Type_Additional") & "") Is Nothing) Then
                        radioType.SelectedValue = ds.Rows(0).Item("Type_Additional") & ""
                    End If
                    If (ds.Rows(0).Item("Type_Additional") & "" = "C") Then
                        rowColor.Visible = True
                        rowmaker.Visible = False
                    ElseIf (ds.Rows(0).Item("Type_Additional") & "" = "M") Then
                        rowColor.Visible = False
                        rowmaker.Visible = True
                    Else
                        rowmaker.Visible = False
                        rowColor.Visible = False
                    End If
                    If Not (ddlColor.Items.FindByValue(ds.Rows(0).Item("Color") & "") Is Nothing) Then
                        ddlColor.SelectedValue = ds.Rows(0).Item("Color") & ""
                    End If
                    ds = clsGet.Get_Stock_Relation(Session("Stock") & "")
                    ViewState("Color") = ds
                    gridProductPoint.DataSource = ds
                    gridProductPoint.DataBind()
                End If
            End If
        End If
    End Sub
    Private Function CreateDatatable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Product_no", GetType(String))
        dt.Columns.Add("Product_name", GetType(String))
        dt.Columns.Add("Group_no", GetType(String))
        dt.Columns.Add("Color_Point", GetType(String))
        dt.Columns.Add("Marker_Point", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub radioType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioType.SelectedIndexChanged
        If (radioType.SelectedValue = "C") Then
            rowColor.Visible = True
        Else
            rowColor.Visible = False
        End If
        If (radioType.SelectedValue = "M") Then
            Label3.Text = "Kilo/Million Liter"
            rowmaker.Visible = True
        Else
            Label3.Text = "PPM"
            rowmaker.Visible = False
        End If
    End Sub


    Private Sub btnSavePoint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSavePoint.Click
        If (ddlGroup.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Product Group !!');", True)
            Exit Sub
        End If
        If (ddlProduct.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Product !!');", True)
            Exit Sub
        End If
        If (txtPoint.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ ความเข้มข้น !!');", True)
            Exit Sub
        End If
        If (radioType.SelectedValue = "M") Then
            If (txtCompute.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ ตัวคูณกรณีเติมเพิ่ม !!');", True)
                Exit Sub
            End If
        End If
        Dim dt As DataTable = ViewState("Color")
        If (ViewState("index") & "" = "") Then
            Dim dr As DataRow = dt.NewRow
            dr.Item("Product_no") = ddlProduct.SelectedValue
            dr.Item("Product_name") = ddlProduct.SelectedItem.Text
            dr.Item("Group_no") = ddlGroup.SelectedValue
            dr.Item("Color_Point") = txtPoint.Text
            dr.Item("Marker_Point") = txtCompute.Text
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Else
            Dim index As Integer = ViewState("index")
            dt.Rows(index).Item("Product_no") = ddlProduct.SelectedValue
            dt.Rows(index).Item("Product_name") = ddlProduct.SelectedItem.Text
            dt.Rows(index).Item("Group_no") = ddlGroup.SelectedValue
            dt.Rows(index).Item("Color_Point") = txtPoint.Text
            dt.Rows(index).Item("Marker_Point") = txtCompute.Text
            dt.AcceptChanges()
        End If
        ViewState("index") = ""
        gridProductPoint.DataSource = dt
        gridProductPoint.DataBind()
        ddlGroup.ClearSelection()
        ddlProduct.ClearSelection()
        ddlProduct.Items.Clear()
        txtPoint.Text = String.Empty
        txtCompute.Text = "1"
        ViewState("Color") = dt
    End Sub

    Private Sub ddlGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroup.SelectedIndexChanged
        Dim ds As DataTable = clsGet.Get_PRODUCT("", ddlGroup.SelectedValue).Tables(0)
        If (ds.Rows.Count > 0) Then
            With ddlProduct
                .ClearSelection()
                .DataTextField = "Product_name"
                .DataValueField = "Product_no"
                .DataSource = ds
                .DataBind()
                .Items.Insert(0, "<--ทั้งหมด-->")
                .Items(0).Value = "0"
            End With
        End If
    End Sub

    Private Sub gridProductPoint_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridProductPoint.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridProductPoint.PageIndex <> 0) Then
            Index = Index Mod (gridProductPoint.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            txtPoint.Text = gridProductPoint.DataKeys(Index).Values("Color_Point") & ""
            If (radioType.SelectedValue = "M") Then
                txtCompute.Text = gridProductPoint.DataKeys(Index).Values("Marker_Point") & ""
            End If
            If Not (ddlGroup.Items.FindByValue(gridProductPoint.DataKeys(Index).Values("Group_no") & "") Is Nothing) Then
                ViewState("index") = Index
                ddlGroup.SelectedValue = gridProductPoint.DataKeys(Index).Values("Group_no") & ""
                Dim ds As DataTable = clsGet.Get_PRODUCT("", ddlGroup.SelectedValue).Tables(0)
                If (ds.Rows.Count > 0) Then
                    With ddlProduct
                        .ClearSelection()
                        .DataTextField = "Product_name"
                        .DataValueField = "Product_no"
                        .DataSource = ds
                        .DataBind()
                        .Items.Insert(0, "<--ทั้งหมด-->")
                        .Items(0).Value = "0"
                    End With
                End If
                If Not (ddlProduct.Items.FindByValue(gridProductPoint.DataKeys(Index).Values("Product_no") & "") Is Nothing) Then
                    ddlProduct.SelectedValue = gridProductPoint.DataKeys(Index).Values("Product_no") & ""
                End If
            End If
        ElseIf e.CommandName = "cmddelete" Then
            Dim dt As DataTable = ViewState("Color")
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("Color") = dt
            gridProductPoint.DataSource = dt
            gridProductPoint.DataBind()
        End If
    End Sub

    Private Sub gridProductPoint_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridProductPoint.RowEditing

    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtGrade.Text = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Grade !!');", True)
            Exit Sub
        End If
        If (txtBrand.Text = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Brand !!');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim color As String = ""
        If (radioType.SelectedValue = "C") Then
            If (ddlColor.SelectedValue = "0") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Color !!');", True)
                Exit Sub
            Else
                color = ddlColor.SelectedValue
            End If
        End If
        Dim dt As DataTable = ViewState("Color")
        For Each dr As DataRow In dt.Rows
            If (clsGet.CHeck_StockDuplicate(ViewState("Stock") & "", radioType.SelectedValue, dr.Item("Product_no"), color) > 0) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('พบข้อมูลสินค้า " & dr.Item("Product_name") & "ในรายการอื่นแล้วกรุณาทำการเลือกสินค้าใหม่ !!');", True)
                Exit Sub
            End If
        Next

        Application.Lock()
        conn.Open()
        Dim Stock_Code As String = ""
        Dim Amount As String = ""
        If ViewState("Stock") & "" = "" Then
            Stock_Code = clsDB.GeneratorID(conn, "Stock")
            Amount = "0"
        Else
            Stock_Code = ViewState("Stock").ToString()
            Amount = ""
        End If

        tran = clsDB.BeginTran(conn)
        If (clsFc.Insert_Stock(ViewState("Stock") & "", Stock_Code, radioType.SelectedValue, color, txtGrade.Text, txtBrand.Text, txtDescription.Text, Amount, conn, tran) > 0) Then
            clsFc.DeleteStock_Relation(Stock_Code, conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.Insert_Stock_Relation(Stock_Code, CInt(dr.Item("Group_no")).ToString("##############00000"), CInt(dr.Item("Product_no")).ToString("#############00000"), dr.Item("Product_name"), dr.Item("Color_Point"), dr.Item("Marker_Point"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            Session("Stock") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("Stock") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub
End Class